package com.example.managesystem.web;


import com.example.managesystem.entity.backGround;
import com.example.managesystem.entity.headImages;
import com.example.managesystem.entity.post;
import com.example.managesystem.service.imageService;
import com.example.managesystem.service.impl.postServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("index")
public class postController {
    @Autowired
    private postServiceImpl PostServiceImpl;
    @Autowired
    private imageService imageService;

    /**
     * @description:  添加添加blog文章
     * @param: [post]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/22 23:22
     */
    @RequestMapping("/PostAdd")
    public String PostAdd(post post, HttpSession session){
        post.setAuthor((String) session.getAttribute("author"));
        post.setHead_portrait((String) session.getAttribute("headImage"));
        PostServiceImpl.add(post);
        return "redirect:/index/components-blog-posts";
    }
    /**
     * @description:  个人blog信息
     * @param: [mod, session]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/24 7:24
     */
    @RequestMapping("/index")
    public String index(ModelMap mod,HttpSession session){
        String author = (String) session.getAttribute("author");
        List<backGround> imagelist=imageService.selectBack();
        mod.addAttribute("backList",imagelist);
        List<post> list= PostServiceImpl.selectPost();
        List<post> list1 = new ArrayList<post>();
        for(int i =0;i<list.size();i++){
            post post=list.get(i);
            if(post.getAuthor().equals(author)){
                list1.add(post);
            }
        }
        mod.addAttribute("postList",list1);
        return "/index";
    }
    /**
     * @description:  更新blog
     * @param: [post]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/23 14:15
     */
    @RequestMapping("/updatePost")
    public String updatePost(post post){
        PostServiceImpl.update(post);
        return "redirect:/index/index";
    }

        /**
         * @description: blog页面显示内容
         * @param: [mod]
         * @return: java.lang.String
         * @author Yang
         * @date: 2022/6/22 23:20
         */
    @RequestMapping("/components-blog-posts")
    public String city(ModelMap mod){
        List<backGround> imagelist=imageService.selectBack();
        List<headImages> imagelist1=imageService.selectImages();
        mod.addAttribute("backList",imagelist);
        mod.addAttribute("imageList",imagelist1);
        List<post> list= PostServiceImpl.selectPost();
        List<post> list1 = list.subList(0,4);
        mod.addAttribute("postList",list1);
        List<post> list2 = list.subList(5,7);
        mod.addAttribute("postList2",list2);
        List<post> list3 = list.subList(8,11);
        mod.addAttribute("postList3",list3);
        List<post> list4 = list.subList(11,list.size());
        mod.addAttribute("postList4",list4);
        return "/components-blog-posts";
    }

    /**
     * @description:  删除blog文章
     * @param: [title]
     * @return: java.lang.String
     * @author Yang
     * @date: 2022/6/22 23:25
     */
    @RequestMapping("/deletePost/{title}")
    public String deletePost(@PathVariable("title") String title){
        PostServiceImpl.delete(title);
        return "redirect:/index/components-blog-posts";
    }
}
